﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Commerce.Common;


namespace CommerceForUmbraco.adminSection.commerce
{
    public partial class ordersCSV : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int filter = int.Parse(Request["f"]);
            DateTime startDateParam = DateTime.Parse(Request["sd"]);
            DateTime endDateParam = DateTime.Parse(Request["ed"]);

            // get collection values
            Commerce.Common.OrderCollection orders = null;

            //Assmeble start and end dates.

            DateTime startDate;
            DateTime endDate = DateTime.Now;


            int year = DateTime.Today.Year;
            int month = DateTime.Today.Month;
            int day = DateTime.Today.Day;


            switch (filter)
            {
                case 1: // Yesterday
                    startDate = new DateTime(year, month, day).AddDays(-1);
                    endDate = new DateTime(year, month, day, 23, 59, 59).AddDays(-1);

                    break;

                case 2: //This week from Monday

                    DayOfWeek dayOfWeek = DateTime.Now.DayOfWeek;
                    int daysOffset = dayOfWeek - DayOfWeek.Monday;
                    DateTime start = DateTime.Now.AddDays(-daysOffset);

                    startDate = new DateTime(start.Year, start.Month, start.Day);
                    endDate = new DateTime(year, month, day, 23, 59, 59);
                    break;

                case 3: //This month
                    startDate = new DateTime(year, month, 1);
                    endDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
                    break;

                case 4: //Custom dates
                    startDate = startDateParam;
                    endDate = endDateParam;
                    break;

                default: // Today
                    startDate = new DateTime(year, month, day);
                    endDate = new DateTime(year, month, day, 23, 59, 59);

                    break;
            }


            orders = new OrderCollection().Where("orderStatusID", SubSonic.Comparison.NotEquals, OrderStatus.NotProcessed).Where(Order.Columns.OrderDate, SubSonic.Comparison.GreaterOrEquals, startDate).Where(Order.Columns.OrderDate, SubSonic.Comparison.LessOrEquals, endDate).OrderByDesc("OrderDate").Load();

            StringBuilder csv = new StringBuilder();

            var line = new StringBuilder();

            // Header
            line.AppendFormat("Name");
            line.AppendFormat(",Order Number");
            line.AppendFormat(",Order Date");
            line.AppendFormat(",Order Status");
            line.AppendFormat(",Order Total");
            line.AppendFormat(",Coupon Codes");
            line.AppendFormat(",Card Type");
            line.AppendFormat(",Username");

            csv.AppendLine(line.ToString());

            // Data
            foreach (var order in orders)
            {
                var dataline = new StringBuilder();
                dataline.AppendFormat("{0} {1}", order.FirstName, order.LastName);
                dataline.AppendFormat(",{0}", order.OrderNumber);
                dataline.AppendFormat(",{0}", order.OrderDate);
                dataline.AppendFormat(",{0}", order.OrderStatus);
                dataline.AppendFormat(",{0}", order.OrderTotal);
                dataline.AppendFormat(",{0}", order.CouponCodes);
                dataline.AppendFormat(",{0}", order.CreditCardType);
                dataline.AppendFormat(",{0}", order.UserName);

                csv.AppendLine(dataline.ToString());
            }

            string attachment = "attachment; filename=PersonList.csv";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");

            HttpContext.Current.Response.Write(csv.ToString());

            HttpContext.Current.Response.End();
        }
    }
}
